package waal.pc.frontend;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import waal.pc.frontend.tiles.EmtyTile;
import waal.pc.frontend.tiles.Tile;

public class TileTopTile extends JPanel implements ActionListener, ComponentListener {

	private static final long serialVersionUID = 1L;
	private static Border border = new BevelBorder(BevelBorder.RAISED);
	private int number; 
	private Tile tile;
	private static Icon closeIcon = createImageIcon("/waal/pc/frontend/images/closeIcon.jpg", "Close Tile");
	
	public TileTopTile(int number, Tile tile) {
		super(new BorderLayout());
		this.addComponentListener(this);
		this.number = number;
		this.tile = tile;
		this.add(this.createHeadline(), BorderLayout.NORTH);
		this.add(tile, BorderLayout.CENTER);	
		this.setBorder(TileTopTile.border);
	}
	
	public TileTopTile(int number) {
		this(number, new EmtyTile(number).getTile());
	}
	
	public void setNewTile(Tile tile) {
		this.tile.closeTile();
		this.tile = tile;
		this.removeAll();
		tile.setDimension(this.getWidth(), this.getHeight()-22);
		this.add(this.createHeadline(), BorderLayout.NORTH);
		this.add(tile, BorderLayout.CENTER);
		this.revalidate();
		this.repaint();	}
	
	public void setEmptyTile() {
		this.setNewTile(new EmtyTile(this.number).getTile());
	}
	
	private JPanel createHeadline() {
		JPanel head = new JPanel(new BorderLayout());
		head.add(new JLabel("[" + this.tile.getID() + "]"), BorderLayout.WEST);
		head.add(new JLabel(this.tile.toString(), SwingConstants.CENTER), BorderLayout.CENTER);
		JButton exitButton = new JButton(TileTopTile.closeIcon);
		exitButton.addActionListener(this);
		exitButton.setPreferredSize(new Dimension(21,21));
		exitButton.setMaximumSize(new Dimension(21,21));
		head.add(exitButton, BorderLayout.EAST);
		head.setBorder( TileTopTile.border );
		
		return head;
	}
	
	protected static ImageIcon createImageIcon(String path, String description) {
		java.net.URL imgURL = TileTopTile.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL, description);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		this.tile.closeTile();
		this.setEmptyTile();
		//TODO default Tile;
	}

	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentResized(ComponentEvent e) {
		tile.setDimension(this.getWidth(), this.getHeight()-22);
	}

	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

}
